//
//  WorksDetailViewController.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 9/20/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "WorksDetailViewController.h"

#import "TaoCanDetailViewController.h"
#import "WorksDetailCell.h"
#import "CameraManDetailViewController.h"


#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "UIImageView+MJWebCache.h"

@interface WorksDetailViewController ()

@end

@implementation WorksDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.titleLabel.text = self.SeriesName;
    self.titleLabel.text = @"作品详情";
    
    imageArray = [[NSMutableArray alloc] init];
    
    [self initViews];
    [self getDetailDatas];
}

#pragma mark UI布局
-(void)initViews
{
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing =0;
    flowLayout.minimumLineSpacing =3;
    flowLayout.headerReferenceSize = CGSizeMake(UI_SCREEN_WIDTH, 152/2.0);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    mCollectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:flowLayout];
    [mCollectionView registerClass:[WorksDetailCell class] forCellWithReuseIdentifier:@"cell"];
    mCollectionView.backgroundColor = mClearColor;
    mCollectionView.alwaysBounceVertical = YES;
    mCollectionView.delegate = self;
    mCollectionView.dataSource = self;
    [self.contentView addSubview:mCollectionView];
    
    
    
    headBgIV = [[MHeadView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 152/2.0f)];
    headBgIV.image = LOADIMAGE(@"bg1@2x");
    headBgIV.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerClick:)];
    [headBgIV addGestureRecognizer:tap];
    
    UIImage * image = [UIImage imageNamed:@"taocanimg"];
    [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    UIButton * taocanBtn = [UIButton initButton:CGRectMake(400/2.0f, 49/2.0f,100, 30) btnImage:image btnTitle:@"预约该摄影师" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:14]];
    taocanBtn.userInteractionEnabled = NO;
//    [taocanBtn addTarget:self action:@selector(taocanClick:) forControlEvents:UIControlEventTouchUpInside];
    [headBgIV addSubview:taocanBtn];
    
    [mCollectionView addSubview:headBgIV];
}


#pragma mark UICollectionView Delegate
//设置分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//每个分区上的元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [imageArray count];
}

//设置元素内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    WorksDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    NSDictionary * dict = [imageArray objectAtIndex:indexPath.row];
    
    [cell.imageIV sd_setImageWithURL:[NSURL URLWithString:[dict valueForKey:@"Photo"]] placeholderImage:ivDetaultImg];
    
    return cell;
}

//设置元素的的大小框
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UIEdgeInsets top ={3,5,3,5};
    return top;
}



//设置元素大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100,100);
}

//点击元素触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WorksDetailCell * cell = (WorksDetailCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if([imageArray  count]){
        int count = imageArray.count;
        // 1.封装图片数据
        NSMutableArray * photos = [NSMutableArray arrayWithCapacity:count];
        for (int i = 0; i<count; i++) {
            // 替换为中等尺寸图片
            NSString *url = imageArray[i][@"Photo"];
            MJPhoto * photo = [[MJPhoto alloc] init];
            photo.url = [NSURL URLWithString:url]; // 图片路径
//            photo.srcImageView = cell.imageIV;
            [photos addObject:photo];
            
        }
        
        // 2.显示相册
        MJPhotoBrowser * browser = [[MJPhotoBrowser alloc] init];
        browser.currentPhotoIndex = indexPath.row;
        browser.photos = photos; // 设置所有的图片
//        browser.rootIV = cell.imageIV;
        [browser show];
    }
    
}


#pragma mark
#pragma mark 点击摄影师头像
-(void)headerClick:(UITapGestureRecognizer *)tap
{
    DDLOG_CURRENT_METHOD;
    CameraManDetailViewController * cvc = [[CameraManDetailViewController alloc] init];
    cvc.type = 1;
    cvc.cameraManID = self.EmplID;

    [self.navigationController pushViewController:cvc animated:YES];
}

#pragma mark
#pragma mark 点击查看套餐详情
-(void)taocanClick:(UIButton *)button
{
    TaoCanDetailViewController * TaoCanVC = [[TaoCanDetailViewController alloc] init];
    TaoCanVC.SeriesID = self.SeriesID;
    TaoCanVC.EmplID = self.EmplID;
    TaoCanVC.SceneID = self.SceneID;
    TaoCanVC.sceneName = self.SceneName;
    TaoCanVC.SeriesName = self.SeriesName;
    TaoCanVC.type = 1;
    [self.navigationController pushViewController:TaoCanVC animated:YES];
}

#pragma mark
#pragma mark 获取详情数据
-(void)getDetailDatas
{
    if (!_isFromCameraMan) {
        if (!(IS_NOT_EMPTY(self.SeriesID)&&IS_NOT_EMPTY(self.EmplID)&&IS_NOT_EMPTY(self.SceneID))){
            return;
        }
        
        if (![XDTools NetworkReachable]){
            [XDTools showTips:brokenNetwork toView:self.view];
            return;
        }
    }
    
    
    //SeriesID 套餐ID EmplID 摄影师ID  SceneID 场景ID
    NSDictionary * parms = @{@"SeriesID":self.SeriesID,
                             @"EmplID":self.EmplID,
                             @"SceneID":self.SceneID};
    
    ASIHTTPRequest * request = [XDTools postRequestWithDict:parms API:works_detail];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.contentView];
        NSDictionary * tmpDict = [XDTools JSonFromString:[mrequest responseString]];
        if ([tmpDict[@"status"] intValue]==1){
            [imageArray removeAllObjects];
            [imageArray addObjectsFromArray:tmpDict[@"data"][@"PhotoList"]];
            
            [headBgIV.emplIV sd_setImageWithURL:[NSURL URLWithString:tmpDict[@"data"][@"EmplAvator"]] placeholderImage:LOADIMAGE(@"placeholderHeader@2x")];
            headBgIV.emplNameLB.text = tmpDict[@"data"][@"EmplName"];
            headBgIV.emplLeveLB.text = tmpDict[@"data"][@"LevelName"];
            
            [mCollectionView reloadData];
            
            //值的存储 -- 摄影师
            [XDTools sharedXDTools].emplImgUrl = tmpDict[@"data"][@"EmplAvator"];
            [XDTools sharedXDTools].emplLevel = tmpDict[@"data"][@"LevelName"];
            [XDTools sharedXDTools].emplName =tmpDict[@"data"][@"EmplName"];

            if(ARRAY_IS_NOT_EMPTY(tmpDict[@"data"][@"ReviewTypeAvg"])){

                [XDTools sharedXDTools].emplZhuanYe = [[tmpDict[@"data"][@"ReviewTypeAvg"] firstObject] valueForKey:@"Avg"];
                [XDTools sharedXDTools].emplGouTong =[[tmpDict[@"data"][@"ReviewTypeAvg"] objectAtIndex:1] valueForKey:@"Avg"];
                [XDTools sharedXDTools].emplShoushi =[[tmpDict[@"data"][@"ReviewTypeAvg"] lastObject] valueForKey:@"Avg"];

            }else{
                [XDTools sharedXDTools].emplZhuanYe = nil;
                [XDTools sharedXDTools].emplGouTong =nil;
                [XDTools sharedXDTools].emplShoushi =nil;
            }
        }else{
            [XDTools showTips:tmpDict[@"msg"] toView:self.view];
        }
    }];
    [request setFailedBlock:^{
        [XDTools hideProgress:self.contentView];
        [XDTools showTips:networkovertime toView:self.view];
    }];
    [request startAsynchronous];
    [XDTools showProgress:self.contentView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
